package com.purong.purong.activity;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.adapter.BankWebSiteAdapter;
import com.purong.purong.bean.AreaListBean;
import com.purong.purong.bean.BankWebSiteBean;
import com.purong.purong.bean.BankWebSiteBean.WebSiteBean;
import com.purong.purong.bean.AreaBean;
import com.purong.purong.util.SharePreUtil;
import com.purong.purong.widget.ChoseAreaDialog;
import com.purong.purong.widget.LoadingView;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import in.srain.cube.views.ptr.PtrCustomHandler;
import in.srain.cube.views.ptr.PtrDefaultCustomHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrPuRongLayout;

/**
 * Created by wenjiezeng on 2017/9/29.
 */
public class BankWebSiteActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private BankWebSiteAdapter mAdapter;
    private TextView titleTv;
    private ImageView titleImg;
    private ArrayList<WebSiteBean> webSiteList=new ArrayList<>();
    private ArrayList<AreaBean> areaList;
    private ChoseAreaDialog dialog;
    private PtrPuRongLayout mPtrFrame;
    private LoadingView loadView;
    private int page=1;
    private String bankId,cityId;
    private AreaBean areaBean;


    @Override
    public void initBundle() {
        bankId=getIntent().getStringExtra("BANK_ID");
        cityId= SharePreUtil.getInstance(this).getLoactionAreaId();
    }

    @Override
    public int getRootViewId() {
        return R.layout.activity_bank_website;
    }

    public void initView(){
        titleImg=(ImageView)findViewById(R.id.title_img);
        titleTv=(TextView)findViewById(R.id.title_tv);
        loadView=(LoadingView)findViewById(R.id.loading_view);
        loadView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getWebSite(true);
            }

            @Override
            public void onRefresh() {

            }
        });
        mPtrFrame = (PtrPuRongLayout) findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setPtrHandler(new PtrCustomHandler() {
            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {

                if (webSiteList.size()==0){
                    return false;
                }else {
                    return PtrDefaultCustomHandler.checkContentCanBePulledUp(frame, content, footer);
                }

            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getWebSite(false);

            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getWebSite(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return (webSiteList.size()>0)?false:true;
            }
        });
        // the following are default settings
        mRecyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        mAdapter=new BankWebSiteAdapter(this);
        mAdapter.setData(webSiteList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void initData() {
        getArea("1");
        getWebSite(true);
    }

    private void setStatus(TextView tv,ImageView iv,int status){
        //status 1:下黑，2：下蓝，3：上黑，4：上篮
        switch (status){
            case 1:
                tv.setTextColor(getResources().getColor(R.color.text_dark_gray));
                iv.setImageResource(R.drawable.cart_black_down);
                break;
            case 2:
                tv.setTextColor(getResources().getColor(R.color.blue));
                iv.setImageResource(R.drawable.cart_blue_down);
                break;
            case 3:
                tv.setTextColor(getResources().getColor(R.color.text_dark_gray));
                iv.setImageResource(R.drawable.cart_black_up);
                break;
            case 4:
                tv.setTextColor(getResources().getColor(R.color.blue));
                iv.setImageResource(R.drawable.cart_blue_top);
                break;
        }
    }
    private void initAreaDialog(){
        dialog=new ChoseAreaDialog(this, areaList, new ChoseAreaDialog.OnAreaChoseListener() {
            @Override
            public void onChose(int position) {
                areaBean=areaList.get(position);
                getWebSite(true);
            }

            @Override
            public void onShow() {

            }

            @Override
            public void onDismiss() {
                if (areaBean!=null){
                    if (areaBean.getArea_id().equals("-1")){
                        titleTv.setText("全部区域");
                        setStatus(titleTv,titleImg,1);
                    }else {
                        titleTv.setText(areaBean.getAcronym());
                        setStatus(titleTv,titleImg,2);
                    }

                }else {
                    setStatus(titleTv,titleImg,1);
                }
            }
        });
    }
    private void getWebSite(boolean clear){
        if (bankId==null||cityId==null)
            return;
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("bank_id",bankId);
        params.put("city_id",cityId);
        if (areaBean!=null&&!areaBean.getArea_id().equals("-1")){
            params.put("area_id",areaBean.getArea_id());
        }else {
            params.put("area_id","");
        }
        if (clear){
            page=1;
            loadView.loading();
            webSiteList.clear();
            mAdapter.notifyDataSetChanged();
        }
        params.put("page",page+"");
        Log.e("zwj","...page="+page);
        if (!request(Contant.BANK_WEBSITE, params, true)) {
            if (clear)
            loadView.fail();
            if (!hasDestroy())
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (mPtrFrame.isRefreshing()) {
                            mPtrFrame.refreshComplete();
                        }
                    }
                }, 100);
        }

    }
    private void getArea(String needShow){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("need_show",needShow);
        params.put("parent_id",cityId);
        if (needShow.equals("1")){
            request(Contant.AREA, params, true, false);
        }else {
            request(Contant.AREA, params, true, true);

        }
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.AREA)){
            if (responseInfo.isRequestOk){
                try{
                    AreaListBean bean= JSON.parseObject(responseInfo.content,AreaListBean.class);
                    if (bean!=null&&bean.getArea_list()!=null){
                        areaList=new ArrayList<>();
                        areaList.addAll(bean.getArea_list());
                        AreaBean defArea=new AreaBean();
                        defArea.setArea_id("-1");
                        defArea.setAcronym("全部区域");
                        defArea.setCheck(true);
                        areaList.add(0,defArea);
                        if (responseInfo.params.get("need_show").equals("2")){
                           initAreaDialog();
                            dialog.show();
                        }
                    }
                }catch (Exception e){

                }
            }
        }else if (responseInfo.url.equals(Contant.BANK_WEBSITE)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try{
                    BankWebSiteBean bean=JSON.parseObject(responseInfo.content,BankWebSiteBean.class);
                    if (bean!=null&&bean.getBank_website_list()!=null){
                        webSiteList.addAll(bean.getBank_website_list());
                        if (bean.getBank_website_list().size()>0)
                            page++;
                        mAdapter.notifyDataSetChanged();
                    }
                    loadView.finish();
                }catch (Exception e){
                    if (page==1)
                        loadView.fail();
                }
            }else {
                if (page==1)
                    loadView.fail();
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.title_bar:
                if (dialog==null){
                    if (areaList==null){
                        getArea("2");
                    }else {
                       initAreaDialog();
                        dialog.show();
                    }
                }else {
                    dialog.show();
                }
                break;
            case R.id.left_back:
                this.finish();
                break;
        }
    }
}
